package controller

import (
	myConfig "common/config"
	"common/helper"
	myredis "common/redis"
	"common/userHelp"
	"github.com/valyala/fasthttp"
	"member/model"
	"time"
)

/*
一键领取VIP所有奖励
*/
func (that MemberController) GetVipBonus(ctx *fasthttp.RequestCtx) {
	var TotalBonus float64 = 0.0
	valUid := model.GetUidFromToken(ctx)

	lockKey := valUid + "VipBonus"
	if !myredis.Lock(model.GetRedisInstance(), lockKey) {
		helper.GetloggerInstance().Criticalf("lockError:%s", lockKey)
		return
	}

	defer myredis.Unlock(model.GetRedisInstance(), lockKey)

	level := userHelp.GetVipLevel(valUid)
	if userHelp.CanWeekBonus(valUid) {
		WeekBonus := myConfig.GetVipWeekBonus(level)
		TotalBonus += WeekBonus
		userHelp.SetWeekBonusTime(valUid)
		userHelp.AddBalanceTransactionRecord(valUid, WeekBonus, helper.TransactionVipWeekBonus, "0", helper.GenId(), 0.00, "", "", 0)
		userHelp.AddUserWageItem(valUid, WeekBonus, helper.TransactionVipWeekBonus)
		_, errExec := model.GetMasterDBInstance().Exec("CALL tbl_report_user_change_update(?,?,?,?)", valUid,
			helper.TransactionVipWeekBonus, WeekBonus, time.Now().Unix())
		if errExec != nil {
			helper.GetloggerInstance().Emergencyf("CALL tbl_report_user_change_update(%s,%d,%0.2f,%d);	reason:%s",
				valUid, helper.TransactionVipWeekBonus, WeekBonus, time.Now().Unix(), errExec.Error())
		}

		userHelp.AddUserBalanceByFloat64(valUid, WeekBonus)
	}

	if userHelp.CanMonthBonus(valUid) {
		MonthBonus := myConfig.GetVipMonthBonus(level)
		TotalBonus += MonthBonus
		userHelp.SetMonthBonusTime(valUid)
		userHelp.AddBalanceTransactionRecord(valUid, MonthBonus, helper.TransactionVipMonthBonus, "0", helper.GenId(), 0.00, "", "", 0)
		userHelp.AddUserWageItem(valUid, MonthBonus, helper.TransactionVipMonthBonus)
		_, errExec := model.GetMasterDBInstance().Exec("CALL tbl_report_user_change_update(?,?,?,?)", valUid,
			helper.TransactionVipMonthBonus, MonthBonus, time.Now().Unix())
		if errExec != nil {
			helper.GetloggerInstance().Emergencyf("CALL tbl_report_user_change_update(%s,%d,%0.2f,%d);	reason:%s",
				valUid, helper.TransactionVipMonthBonus, MonthBonus, time.Now().Unix(), errExec.Error())
		}

		userHelp.AddUserBalanceByFloat64(valUid, MonthBonus)
	}

	UpBonusLevel := userHelp.GetUpBonusLevel(valUid)
	if UpBonusLevel < level {
		var TotalUpBonus float64 = 0.00
		for Index := UpBonusLevel + 1; Index <= level; Index++ {
			UpBonus := myConfig.GetVipUpBonus(Index)
			TotalUpBonus += UpBonus
		}

		userHelp.SetUpBonusLevel(valUid, level)

		userHelp.AddBalanceTransactionRecord(valUid, TotalUpBonus, helper.TransactionVipUpBonus, "0", helper.GenId(), 0.00, "", "", 0)
		userHelp.AddUserWageItem(valUid, TotalUpBonus, helper.TransactionVipUpBonus)
		_, errExec := model.GetMasterDBInstance().Exec("CALL tbl_report_user_change_update(?,?,?,?)", valUid,
			helper.TransactionVipUpBonus, TotalUpBonus, time.Now().Unix())
		if errExec != nil {
			helper.GetloggerInstance().Emergencyf("CALL tbl_report_user_change_update(%s,%d,%0.2f,%d);	reason:%s",
				valUid, helper.TransactionVipUpBonus, TotalUpBonus, time.Now().Unix(), errExec.Error())
		}

		userHelp.AddUserBalanceByFloat64(valUid, TotalUpBonus)
	}

	helper.Print(ctx, true, TotalBonus)
}
